package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @package com.leyou.item.service
 * @description: 查询商品分类的service
 * @author: 许超越
 * @date 2019/6/23 14:51
 * @version: V1.0
 */
@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BrandMapper brandMapper;

    public List<CategoryDTO> queryByParentId(Long id) {
        //将查询条件封装进category对象中
        Category category = new Category();
        category.setParentId(id);
        //调用方法查询
        List<Category> categories = categoryMapper.select(category);
        //对查询结果判断，查询为空抛出空异常
        if (CollectionUtils.isEmpty(categories)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        //把查询出来的结果转换成CategoryDTO的结合
       return BeanHelper.copyWithCollection(categories, CategoryDTO.class);
    }


    /**
     * 根据品牌id查询查询类目
     * @param bid
     * @return
     */
    public List<CategoryDTO> queryByBrandId(Long bid) {
        //多表查询类目表
        List<Category> list = categoryMapper.queryByBrandId(bid);
        //判断查询结果
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //返回查询结果
        return BeanHelper.copyWithCollection(list, CategoryDTO.class);
    }


    /**
     * 根据品牌id删除商品及类目的中间表
     * @param bid
     */
    @Transactional
    public void deleteByBrandId(Long bid) {
        //删除brand表中的数据
        int delBrandCount = brandMapper.deleteByPrimaryKey(bid);
        if (delBrandCount != 1) {
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
        //计算中间表中记录数量
        int count = categoryMapper.queryCountByBrandId(bid);
        //通过bid删除中间表
        int delInterCount =  categoryMapper.deleteIntermediateByBrandId(bid);
        if (delInterCount != count) {
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }


    /**
     * 通过分类id的集合查询分类
     * @param: ids 分类的集合
     * @return
     */
    public List<CategoryDTO> queryCategoryByIds(List<Long> ids){
        //查询商品分类
        List<Category> categoryList = categoryMapper.selectByIdList(ids);
        //判断是否查询到
        if (CollectionUtils.isEmpty(categoryList)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categoryList, CategoryDTO.class);
    }


    /**
     * 根据3级分类id查询所有分类
     * @param: cid3 3级分类id
     * @return
     */
    public List<CategoryDTO> queryCategoryByCid3(Long cid3) {
        //根据id查询3级分类对象
        Category category3 = categoryMapper.selectByPrimaryKey(cid3);
        //判断是否查询到
        if (category3 == null) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        //获取2级分类id，查询对应实体类，并判断是否查询到
        Category category2 = categoryMapper.selectByPrimaryKey(category3.getParentId());
        if (category2 == null) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        //获取1级分类id，查询对应实体类，并判断是否查询到
        Category category1 = categoryMapper.selectByPrimaryKey(category2.getParentId());
        if (category1 == null) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        //将获取到的3个分类对象封装进集合
        List<Category> categoryList = Arrays.asList(category1, category2, category3);
        //转换成DTO类型的集合返回
        return BeanHelper.copyWithCollection(categoryList, CategoryDTO.class);
    }
}
